package com.ifcreate.infowell.repository;

import com.ifcreate.infowell.entity.InfoWellNode;
import com.ifcreate.infowell.entity.InfoWellRelation;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author chenzedeng
 * @Email yustart@foxmail.com
 * @Create 2020-10-25 4:46 下午
 */
@Repository
@Transactional(rollbackFor = Exception.class)
public interface InfoWellRepository extends Neo4jRepository<InfoWellNode, Long> {


    /**
     * 删除关系根据关系的ID
     *
     * @param id
     */
    @Query("match ()-[r]-() where id(r) = $0 delete r")
    void deleteRelationshipById(Long id);

    /**
     * 查询这个大节点下第一级深度的子节点
     *
     * @param rootId
     * @return
     */
    @Query("match p=(root)<-[r]-(n) where id(root) = $rootId return p")
    List<InfoWellRelation> queryFirstRelationNodeForNext(Long rootId);


    /**
     * 查询这个大节点下第一级深度的上级子节点
     *
     * @param nodeId
     * @return
     */
    @Query("match p=(n1)<-[r]-(n) where id(n) = $nodeId return p")
    List<InfoWellRelation> queryFirstRelationNodeForLast(Long nodeId);


    /**
     * 查询所有的大的新闻节点
     *
     * @param type
     * @param time
     * @param size
     * @return

     @Query("match ()-[*0..]->(root) where root.isNews = true and root.type = $type and root.time <= $time  return root order by root.time desc limit $size")
     List<InfoWellNode> findRootNode(Integer type, long time, int size); */


    /**
     * 获取两个节点之间的关系
     *
     * @param startId
     * @param endId
     * @return
     */
    @Query("match p=(n)-[r]-(n1) where id(n) = $0 and id(n1) = $1 return p")
    InfoWellRelation getRelationByStartIdAndEndId(Long startId, Long endId);
}
